package com.wbcs.system.component.application.html;

import java.util.List;

import com.wbcs.config.component.IComponentConfigBean;
import com.wbcs.config.component.application.jsphtml.HtmlComponentBean;
import com.wbcs.config.print.PrintSubPageBean;
import com.wbcs.config.template.TemplateBean;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.component.AbsComponentType;
import com.wbcs.system.component.application.AbsApplicationType;
import com.wbcs.system.component.container.AbsContainerType;
import com.wbcs.system.tags.component.AbsComponentTag;
import com.wbcs.util.Consts;
import com.wbcs.util.Tools;

public class HtmlTemplateApp extends AbsApplicationType
{
    private HtmlComponentBean htmlConfigBean;

    public HtmlTemplateApp(AbsContainerType parentContainerType,IComponentConfigBean comCfgBean,ReportRequest rrequest)
    {
        super(parentContainerType,comCfgBean,rrequest);
        htmlConfigBean=(HtmlComponentBean)comCfgBean;
    }
    
    public void initUrl(IComponentConfigBean applicationConfigBean,ReportRequest rrequest)
    {}

    public void displayOnPage(AbsComponentTag displayTag)
    {
        if(!rrequest.checkPermission(this.htmlConfigBean.getId(),null,null,Consts.PERMISSION_TYPE_DISPLAY))
        {
            wresponse.println("&nbsp;");
            return;
        }
        if(htmlConfigBean.getTplBean()==null)
        {
            wresponse.println("&nbsp;");
            return;
        }
        StringBuffer tempBuf=new StringBuffer();
        String width=null;
        if(this.getParentContainerType()!=null)
        {
            width=this.getParentContainerType().getChildDisplayWidth(this.htmlConfigBean);
            if(width==null||width.trim().equals("")) width="100%";
            if(htmlConfigBean.getTop()!=null&&!htmlConfigBean.getTop().trim().equals(""))
            {
                wresponse.println("<table  cellspacing='0' cellpadding='0' width=\""+width+"\" style=\"MARGIN:0;\">");
                wresponse.println("<tr><td height=\""+htmlConfigBean.getTop()+"\">&nbsp;</td></tr></table>");
            }
            tempBuf.append("<table  cellspacing='0' cellpadding='0' width=\""+width+"\" id=\""+htmlConfigBean.getGuid()+"\"");
            if(htmlConfigBean.getHeight()!=null&&!htmlConfigBean.getHeight().trim().equals(""))
            {
                tempBuf.append(" height=\""+htmlConfigBean.getHeight()+"\" ");
            }
            tempBuf.append("><tr><td valign=\"top\">");
            wresponse.println(tempBuf.toString());
        }
        wresponse.println("<span id=\"WX_CONTENT_"+htmlConfigBean.getGuid()+"\">");
        printlnHtmReallValue();
        wresponse.println("</span>");
        if(this.getParentContainerType()!=null)
        {
            wresponse.println("</td></tr></table>");
            if(this.htmlConfigBean.getBottom()!=null&&!this.htmlConfigBean.getBottom().trim().equals(""))
            {
                wresponse.println("<table  cellspacing='0' cellpadding='0' width=\""+width+"\" style=\"MARGIN:0;\">");
                wresponse.println("<tr><td height=\""+this.htmlConfigBean.getBottom()+"\">&nbsp;</td></tr></table>");
            }
        }
    }

    public String getRealParenttitle()
    {
        String parenttitle=htmlConfigBean.getParenttitle(rrequest);
        if(parenttitle==null) return "";
        return parenttitle.trim();
    }

    @Override
	protected String getComponentTypeName()
    {
        return "application.html";
    }

    public void displayOnExportDataFile(Object templateObj,boolean isFirstime)
    {
        displayOnPage(null);
    }
    
    @Override
	public void printApplication(List<PrintSubPageBean> lstPrintPagebeans)
    {
        if(!rrequest.checkPermission(this.htmlConfigBean.getId(),null,null,Consts.PERMISSION_TYPE_DISPLAY))
        {
            wresponse.println("&nbsp;");
            return;
        }
        if(this.htmlConfigBean.getPrintwidth()!=null&&!this.htmlConfigBean.getPrintwidth().trim().equals(""))
        {
            this.wresponse.println("<div width=\""+this.htmlConfigBean.getPrintwidth()+"\">");
        }
        printlnHtmReallValue();
        if(this.htmlConfigBean.getPrintwidth()!=null&&!this.htmlConfigBean.getPrintwidth().trim().equals(""))
        {
            this.wresponse.println("</div>");
        }
    }
    
    private void printlnHtmReallValue()
    {
        AbsComponentType realDisplayComTypeObj=this;
        if(this.htmlConfigBean.getBelongToCcbean()!=null)
        {//当前<html/>属于一个组件，则取到真正的组件类型对象，稍后可能会用自定义标签显示它们的内容
            realDisplayComTypeObj=(AbsComponentType)rrequest.getComponentTypeObj(this.htmlConfigBean.getBelongToCcbean(),null,false);
        }
        if(htmlConfigBean.getTplBean().getLstTagChildren()!=null&&htmlConfigBean.getTplBean().getLstTagChildren().size()>0)
        {
            wresponse.println(htmlConfigBean.getTplBean().getDisplayValue(rrequest,realDisplayComTypeObj));
        }else
        {
            String tplcontent=this.htmlConfigBean.getTplBean().getContent();
            if(tplcontent==null||!Tools.isDefineKey("i18n",tplcontent.trim()))
            {
                wresponse.println(tplcontent);
            }else
            {
                Object obj=rrequest.getI18NObjectValue(tplcontent.trim());
                if(obj==null)
                {
                    wresponse.println("&nbsp;");
                }else
                {
                    if(!(obj instanceof TemplateBean))
                    {
                        wresponse.println(obj.toString());
                    }else
                    {
                        wresponse.println(((TemplateBean)obj).getDisplayValue(rrequest,realDisplayComTypeObj));
                    }
                }
            }
        }
    }
}
